#include "logindialog.h"
#include "ui_logindialog.h"
#include <QMessageBox>
#include "databasemanager.h"

loginDialog::loginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::loginDialog)
{
    ui->setupUi(this);

}

loginDialog::~loginDialog()
{
    delete ui;
}

void loginDialog::on_loginOnPushButton_clicked()
{
    QString username=ui->logUserNameLineEdit->text().trimmed(); //获取输入的用户名，去除首尾空白字符
    QString password=ui->logUserPasswordLineEdit->text(); //获取输入的密码

    if(username.isEmpty()||password.isEmpty()){
        QMessageBox::warning(this,"错误","用户名或密码不能为空！");
        ui->logUserNameLineEdit->setFocus();  //焦点到用户名输入框
    }else{

        if(DatabaseManager::instance().validateUser(username,password)){
            QMessageBox::information(this,"成功","登录成功！");
            accept();
        }else{
            QMessageBox::critical(this,"错误","用户名或密码错误！");
            ui->logUserNameLineEdit->clear();
            ui->logUserPasswordLineEdit->clear();
            ui->logUserNameLineEdit->setFocus();
        }
    }


}


void loginDialog::on_logExitPushButton_clicked()
{
    reject();
}

